import Vue from 'vue'
// import Crypto from './crypto'

const vm = new Vue()

export default {
  /**
   * 获取数据类型
   * @param value 需要判断的值
   * @return {String} 类型   String Number Object......
   */
  getType(value) {
    return Object.prototype.toString.call(value).slice(8, -1)
  },
  /**
   * 获取url参数值
   * @param name  name
   */
  getUrlParam(name) {
    var reg = new RegExp('(^|/?|&)' + name + '=([^&]*)(&|$)', 'i')
    var l = window.location.href
    var i = l.indexOf('?')
    if (i < 0) return null
    var str = l.slice(i + 1, l.length)
    if (str) {
      var r = str.match(reg)
      if (r) {
        return unescape(r[2])
      }
    }
    return null
  },
  /**
   * 设置cookie
   * @param key  key
   * @param value 值
   * @param time  保存时间（天）
   */
  setCookie(key, value, time) {
    var cur = new Date()
    cur.setTime(cur.getTime() + time * 24 * 3600 * 1000)
    document.cookie = key + '=' + encodeURIComponent(value) + ';expires=' + (time ? cur.toGMTString() : '')
  },
  getCookie(key) {
    var data = document.cookie
    var startIndex = data.indexOf(key + '=')
    if (startIndex > -1) {
      startIndex = startIndex + key.length + 1
      var endIndex = data.indexOf(';', startIndex)
      endIndex = endIndex < 0 ? data.length : endIndex
      return decodeURIComponent(data.substring(startIndex, endIndex))
    } else {
      return ''
    }
  },
  delCookie(key) {
    var data = this.getCookie(key)
    if (data !== false) this.setCookie(key, data, -1)
  },
  /**
   *
   * @param key key
   * @param value value
   * @param encode 是否加密存储
   */
  // setSStorage(key, value, encode) {
  //   let str = window.JSON.stringify(value)
  //   if (encode) {
  //     str = Crypto.encrypt(str)
  //   }
  //   window.sessionStorage.setItem(key, str)
  // },
  // getSStorage(key, decode) {
  //   var str = ''
  //   str = window.sessionStorage.getItem(key)
  //   if (!str) return ''
  //   if (decode) {
  //     str = Crypto.decrypt(str)
  //   }
  //   try {
  //     return window.JSON.parse(str)
  //   } catch (error) {
  //     window.sessionStorage.removeItem(key)
  //     window.location.reload()
  //   }
  // },
  // setLStorage(key, value, encode) {
  //   let str = window.JSON.stringify(value)
  //   if (encode) {
  //     str = Crypto.encrypt(str)
  //   }
  //   window.localStorage.setItem(key, str)
  // },
  // getLStorage(key, decode) {
  //   var str = ''
  //   str = window.localStorage.getItem(key)
  //   if (!str) return ''
  //   try {
  //     if (decode) str = Crypto.decrypt(str)
  //     return window.JSON.parse(str)
  //   } catch (error) {
  //     window.localStorage.removeItem(key)
  //     window.location.reload()
  //   }
  // },
  checkMobile(nub) {
    return /^1[0-9]{10}$/.test(nub)
  },
  isNumber(val) {
    const pattern = /^[1-9]{1}\d*$/g
    const isNumber = pattern.test(val)
    return isNumber
  },
  isLink(val) {
    const pattern = /^(?:(?:https?|ftp):\/\/)?(?:[\da-z.-]+)\.(?:[a-z.]{2,6})(?:\/\w\.-]*)*\/?/
    const isLink = pattern.test(val)
    return isLink
  },
  checkLink(val) {
    const isLink = this.isLink(val)
    if (!isLink) vm.$message.error('请输入正确的网址')
  },
  /**
   * 四舍五入强制保留n位小数
   * @param  x  操作数字
   * @param  n 保留位数
   */
  toDecimal(x, n) {
    var f = parseFloat(x)
    if (isNaN(f) && isNaN(n)) {
      return false
    }
    if (n === 0) return Math.round(x)
    var num = Number('1E' + n)
    f = Math.round(x * num) / num
    var s = f.toString()
    var rs = s.indexOf('.')
    if (rs < 0) {
      rs = s.length
      s += '.'
    }
    while (s.length <= rs + n) {
      s += '0'
    }
    return s
  },
  /**
   * 获取日期时间 默认格式 "yyyy-mm-dd hh:mm:ss"
   * @param {String} formatType  可选 格式类型
   * @param {Date} formatDate  可选 指定日期
   * @return {String} 所需格式的日期
   */
  getFormatDate(formatType, formatDate) {
    const type = formatType || 'yyyy-mm-dd hh:mm:ss'
    const date = Object.prototype.toString.call(formatDate) === '[object Date]' ? formatDate : new Date()
    let currentdate = ''
    let month = date.getMonth() + 1
    let strDate = date.getDate()
    let minutes = date.getMinutes()
    let seconds = date.getSeconds()
    month >= 1 && month <= 9 ? (month = '0' + month) : ''
    strDate >= 0 && strDate <= 9 ? (strDate = '0' + strDate) : ''
    minutes >= 0 && minutes <= 9 ? (minutes = '0' + minutes) : ''
    seconds >= 0 && seconds <= 9 ? (seconds = '0' + seconds) : ''

    switch (type.toLowerCase()) {
      case 'yyyy-mm-dd hh:mm:ss':
        currentdate =
          date.getFullYear() + '-' + month + '-' + strDate + ' ' + date.getHours() + ':' + minutes + ':' + seconds
        break
      case 'yyyy-mm-dd':
        currentdate = date.getFullYear() + '-' + month + '-' + strDate
        break
      case 'yyyy-mm-dd hh:mm':
        currentdate = date.getFullYear() + '-' + month + '-' + strDate + ' ' + date.getHours() + ':' + minutes
        break
      case 'yyyy-mm-dd hh':
        currentdate = date.getFullYear() + '-' + month + '-' + strDate + ' ' + date.getHours()
        break
      case 'yyyy-mm':
        currentdate = date.getFullYear() + '-' + month
        break
      case 'yyyy':
        currentdate = date.getFullYear()
        break
      default:
        currentdate =
          date.getFullYear() + '-' + month + '-' + strDate + ' ' + date.getHours() + ':' + minutes + ':' + seconds
        break
    }
    return currentdate
  },
  /**
   * 数据导出
   * @param {String} url 导出路径
   * @param {Object} data  导出参数
   */
  exportData(url, data) {
    if (typeof data !== 'object') {
      console.warn('导出参数错误')
      return
    }
    let queryData = ''
    let downloadUrl = ''
    for (const [key, value] of Object.entries(data)) {
      if (key === 'sessionId') break
      queryData += `${key}=${value}&`
    }
    downloadUrl = url + '?' + queryData.slice(0, -1)
    window.location.href = downloadUrl
  },
  /**
   * 缓冲函数
   * @param {Object} dom 目标dom
   * @param {Number} destination 目标位置
   * @param {Number} rate 缓动率
   * 示例用法
    var dom = document.documentElement || document.body;
    this.$util.easeout(dom, 0, 5);
   */
  easeout(dom, destination = 0, rate = 3) {
    let position = dom.scrollTop
    if (position === destination || typeof destination !== 'number' || rate === 0) {
      return false
    }
    // 不存在原生`requestAnimationFrame`，用`setTimeout`模拟替代
    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = function (fn) {
        return setTimeout(fn, 17)
      }
    }
    const step = function () {
      position = position + (destination - position) / rate
      if (Math.abs(destination - position) < 1) {
        // 动画结束
        dom.scrollTop = destination
        return
      }
      dom.scrollTop = position
      requestAnimationFrame(step)
    }
    step()
  },
  /**
   * 对象深拷贝
   * @param {Object} obj 初始对象
   * @return {Object} result 拷贝后对象
   */
  // deepClone(obj) {
  //   const result = Array.isArray(obj) ? [] : {}
  //   for (const key in obj) {
  //     if (obj.hasOwnProperty(key)) {
  //       if (typeof obj[key] === 'object' && obj[key] !== null) {
  //         result[key] = this.deepClone(obj[key]) // 递归复制
  //       } else {
  //         result[key] = obj[key]
  //       }
  //     }
  //   }
  //   return result
  // },
  /**
   * 获取数据类型
   * @param value 需要判断的值
   */
  getType(value) {
    return Object.prototype.toString.call(value).slice(8, -1)
  },
  /**
   * 设置cookie
   * @param key  key
   * @param value 值
   * @param time  保存时间（天）
   */
  setCookie(key, value, time) {
    var cur = new Date()
    cur.setTime(cur.getTime() + time * 24 * 3600 * 1000)
    document.cookie = key + '=' + encodeURIComponent(value) + ';expires=' + (time ? cur.toGMTString() : '')
  },
  getCookie(key) {
    var data = document.cookie
    var startIndex = data.indexOf(key + '=')
    if (startIndex > -1) {
      startIndex = startIndex + key.length + 1
      var endIndex = data.indexOf(';', startIndex)
      endIndex = endIndex < 0 ? data.length : endIndex
      return decodeURIComponent(data.substring(startIndex, endIndex))
    } else {
      return ''
    }
  },
  delCookie(key) {
    var data = this.getCookie(key)
    if (data !== false) this.setCookie(key, data, -1)
  },
  setSessionStorage(key, value) {
    const str = window.JSON.stringify(value)
    if (window.sessionStorage) {
      window.sessionStorage.setItem(key, str)
    }
  },
  getSessionStorage(key) {
    var json = ''
    if (window.sessionStorage) {
      json = window.sessionStorage.getItem(key)
      if (json) {
        json = window.JSON.parse(json)
      }
    }
    return json
  },
  setLocalStorage(key, value) {
    const str = window.JSON.stringify(value)
    if (window.localStorage) {
      window.localStorage.setItem(key, str)
    }
  },
  getLocalStorage(key) {
    var json = ''
    if (window.localStorage) {
      json = window.localStorage.getItem(key)
      if (json) {
        json = window.JSON.parse(json)
      }
    }
    return json
  },
  /**
   * 获取url参数值
   * @param name  name
   */
  getUrlParam(name) {
    var reg = new RegExp('([?&])' + name + '=([^&|^#]*)')
    var l = window.location.href
    var i = l.indexOf('?')
    if (i < 0) return null
    var str = l.slice(i, l.length)
    if (str) {
      var r = str.match(reg)
      if (r) {
        return decodeURIComponent(r[2])
      }
    }
    return null
  },
  /**
   * 检查手机号是否合法
   * @param {String} phone 手机号
   */
  checkMobile(phone) {
    if (/^1[3-9]\d{9}$/.test(phone)) return true
    return false
  },
  /**
   * 检查网址格式
   * @param {String} val 网址
   */
  isLink(val) {
    const pattern = /^(?:(?:https?|ftp):\/\/)?(?:[\da-z.-]+)\.(?:[a-z.]{2,6})(?:\/\w\.-]*)*\/?/
    const isLink = pattern.test(val)
    return isLink
  },
  jumpLink(link) {
    const isLink = this.isLink(link)
    if (!isLink) return
    window.location.href = link
  },
  /**
   * 滑动到指定位置的缓冲动画函数
   * @param {Object} dom 目标dom
   * @param {Number} destination 目标位置
   * @param {Number} rate 缓动率(越大速度越慢)
   * 示例用法
     let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
     let dom = scrollTop === document.documentElement.scrollTop ? document.documentElement : document.body;
     this.$util.easeout(dom, 0);
   */
  easeout(dom, destination = 0, rate = 5) {
    let position = dom.scrollTop
    if (position === destination || typeof destination !== 'number' || rate === 0) {
      return false
    }
    // 不存在原生`requestAnimationFrame`，用`setTimeout`模拟替代
    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = function (fn) {
        return setTimeout(fn, 17)
      }
    }
    const step = function () {
      position = position + (destination - position) / rate
      if (Math.abs(destination - position) < 1) {
        // 动画结束
        dom.scrollTop = destination
        return
      }
      dom.scrollTop = position
      requestAnimationFrame(step)
    }
    step()
  },
  /**
   * 滚动到指定元素
   * @param {Object} el 当前dom元素
   * @param {Number} offset 元素距离顶部的偏移量
   */
  scrollIntoView(el, offset = 200) {
    if (!el) return
    if (typeof offset !== 'number') return
    const clientRect = el.getBoundingClientRect()
    const isElementInViewport =
      clientRect.top >= 0 && clientRect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
    if (!isElementInViewport) {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      const dom = scrollTop === document.documentElement.scrollTop ? document.documentElement : document.body
      const destination = scrollTop + clientRect.top - offset > 0 ? scrollTop + clientRect.top - offset : 0
      this.easeout(dom, destination, 10)
    }
  },
  /**
   * 执行js代码
   * @param {String} jscode js代码
   * @param {String} scriptid id标识（相同id只执行一次，不传id每次都会执行）
   */
  initScript(jscode, scriptid) {
    if (typeof jscode !== 'string') return
    if (scriptid && document.getElementById(scriptid)) return
    jscode = jscode.replace(/[\r\n]/g, '') // 去除换行
    if (/<script(.*)src=(.*)><\/script>/.test(jscode)) {
      const arr = jscode.match(/src=["|']?(.*?)('|"|>|\\s+)/)
      const script = document.createElement('script')
      script.src = arr[1]
      document.head.appendChild(script)
      return
    }
    const arr = jscode.match(/<script(.*)>(.*)<\/script>/)
    if (Array.isArray(arr) && arr.length >= 3) jscode = arr[2] // 提取js代码
    const script = document.createElement('script')
    if (scriptid) script.id = scriptid
    script.type = 'text/javascript'
    script.innerHTML = jscode
    document.head.appendChild(script)
  },
  /**
   * 判断是微信内还是微信外打开
   */
  isWechat() {
    return /micromessenger/.test(window.navigator.userAgent.toLowerCase())
  },
  /**
   * 文本截取换行
   * @param {String} text 文本字符
   * @param {String} sign 换行标识
   */
  textBr(text, sign = '\n') {
    let brstr = ''
    const arr = text.split(sign)
    arr.forEach(v => {
      brstr += v + '</br>'
    })
    return brstr
  },
  /**
   * px转rem
   * @param {String} str 含 px 字符串  例如：50px 10px 0 10px ; <span style="16px">测试</span>
   * @return {String} 转化后含rem字符串 例如：1rem 0.3rem 0 0.2rem ; <span style="0.32rem">测试</span>
   */
  changeRem(str = '') {
    if (this.getType(str) !== 'String') str = str.toString()
    const nospace = str.trim()
    return nospace.replace(/(-?\d+)(px)/g, (a, b) => {
      return b / 50 + 'rem'
    })
  },
  //   /**
  //    * 格式化对象格式样式 px转rem
  //    * @param {Object} obj 格式化对象
  //    */
  //   formatStyle(obj) {
  //     for (const key in obj) {
  //       if (obj.hasOwnProperty(key) && this.getType(obj[key]) === 'String') {
  //         if (obj[key].includes('px')) obj[key] = this.changeRem(obj[key])
  //       }
  //     }
  //     return obj
  //   }
}
